# Copyright (c) 2015 lizhi

import maya.cmds as mc
import os

import tools_Persional.LZ_Tools.LZ_Tools.check_tools.LZ_cheeckTools.LZ_operations.LZ_rig_check as rig
reload(rig)

class LZ_checkTools_UI(rig.LZ_rig_check):
    def __init__(self):
        currentDir = os.path.dirname(__file__)
        self.iconsDir = os.path.join(currentDir, 'icons')
        self.checksLay = None
        
    def LZ_checkTool_UI(self):
        
        winName = 'LZ_checkTools'
        winTitle = 'LZ_checkTools_v01'
        if mc.window(winName,ex = True):
            mc.deleteUI(winName)
        mc.window( winName , title = winTitle, widthHeight=(330, 410))
        
        self.mainForm = mc.formLayout()
        
        self.assetTypeOM = mc.optionMenu(enable=True)
        mc.optionMenu( self.assetTypeOM , e=True , changeCommand = lambda *args:self.changeAssetType())
        
        
        
        index = 1
        indexToSelect = 0
        
        for eachType in  ['hirig','lowrig'] :
            mc.menuItem(label=eachType)
        #    if eachType == self.assetType:
        #        indexToSelect = index
        #    index = index + 1
        #    
        self.assetClassOM = mc.optionMenu(enable=True)
        mc.optionMenu( self.assetClassOM , e=True , changeCommand = lambda *args:self.changeAssetType())
        
        index = 1
        indexToSelect = 0
        for eachClass in ['creatures', 'prop', 'vehicle', 'env', 'fx']:
            mc.menuItem(label=eachClass)
            #if eachClass == self.assetClass:
            #    indexToSelect = index
            #index = index + 1
            #
        
        
        ## Checks Area
        self.scrollArea = mc.scrollLayout(horizontalScrollBarThickness=16, verticalScrollBarThickness=16, childResizable=True)
        
        assetValue = self.GetOptionMenuType( OptionMenu_name = self.assetTypeOM )

        self.populateChecks(assetValue)
        
        
        ## Run Checks
        checkBTN = mc.button(label='Check', parent=self.mainForm, command = lambda *args:self.LZ_runCheck())

        ## Close UI
        closeBTN = mc.button(label='Close', parent=self.mainForm, command = lambda *args:self.deleteUI(winName))

        ## Edit self.mainForm
        mc.formLayout(self.mainForm, e=True, af=(self.assetTypeOM, 'top', 10))
        mc.formLayout(self.mainForm, e=True, af=(self.assetTypeOM, 'left', 130))
        mc.formLayout(self.mainForm, e=True, af=(self.assetTypeOM, 'right', 130))
        
        mc.formLayout(self.mainForm, e=True, ac=(self.assetClassOM, 'top', 10, self.assetTypeOM))
        mc.formLayout(self.mainForm, e=True, af=(self.assetClassOM, 'left', 130))
        mc.formLayout(self.mainForm, e=True, af=(self.assetClassOM, 'right', 130))
        
        
        mc.formLayout(self.mainForm, e=True, ac=(self.scrollArea, 'top', 10, self.assetClassOM))
        mc.formLayout(self.mainForm, e=True, af=(self.scrollArea, 'left', 0))
        mc.formLayout(self.mainForm, e=True, af=(self.scrollArea, 'right', 0))
        mc.formLayout(self.mainForm, e=True, af=(self.scrollArea, 'bottom', 70))
        
        mc.formLayout(self.mainForm, e=True, ac=(checkBTN, 'top', 10, self.scrollArea))
        mc.formLayout(self.mainForm, e=True, af=(checkBTN, 'left', 70))
        mc.formLayout(self.mainForm, e=True, af=(checkBTN, 'right', 70))

        mc.formLayout(self.mainForm, e=True, ac=(closeBTN, 'top', 3, checkBTN))
        mc.formLayout(self.mainForm, e=True, af=(closeBTN, 'left', 70))
        mc.formLayout(self.mainForm, e=True, af=(closeBTN, 'right', 70))
            
        mc.showWindow( winName)
            
    def changeAssetType( self  ):
        Value = self.GetOptionMenuType( OptionMenu_name = self.assetTypeOM )

        self.populateChecks(  Type = Value)
    
    def changeAssetClass(self, *args):
        Value = self.assetClass = mc.optionMenu(self.assetClassOM, q=True, value=True)   
        
        
        
            
    def GetOptionMenuType(self, OptionMenu_name = '' ):

        assetType = mc.optionMenu( OptionMenu_name , q=True, value=True)   
        return assetType
        
    def populateChecks(self ,Type = ''):
        if self.checksLay:
            if mc.rowColumnLayout(self.checksLay, exists=True):
                mc.deleteUI(self.checksLay)

        self.checksLay = mc.rowColumnLayout(numberOfColumns=3, parent=self.scrollArea)

        self.checksIconsDict = {}

        #checksList = self.getFunctions(self.assetType)
        checksList = self.info_list(  assetType = Type )
        i=1
        for eachCheck in checksList:
            img = mc.iconTextButton(style='iconOnly', image1=os.path.join(self.iconsDir, 'toDoCheck.png'))
            mc.text('  %s.  ' % i, align='right')
            mc.text('%s' % eachCheck, align='left')
            
            self.checksIconsDict[eachCheck] = img
        
            i=i+1
            
        #print(self.checksIconsDict)
    def LZ_runCheck(self):
        
        for cl in self.allCheckList:
            if cl in self.checksIconsDict.keys():
                self.checkControllerAttributesWithInput()
				#if 
                
                img = self.checksIconsDict[cl]
                mc.iconTextButton(img, e=True, image1=os.path.join(self.iconsDir, 'validCheck.png'))
                
                
    
    
    
    def info_list(self, assetType = '' ):
        
        self.allCheckList = ['Check If Reference Exists',
                        'check Non-Unique Names',
                        'Check Controllers Animatable Attributes Input' ,
                        'Check Controllers Animatable Attributes Lock State' ,
                        'Check Controllers non-Animatabal Attributes',
                        'Check non-normalized weights',
                        'Check for disply layers in the scene',
                        'Check ikHandle Vis',
                        'Check Cluter Vis',
                        'Check DefalutValue On Controllers',
                        'Check nameSpace Exists'

                        ]
        
        if assetType == 'hirig':
            Check_list = [self.allCheckList[0],self.allCheckList[1],self.allCheckList[2],self.allCheckList[3],self.allCheckList[4],self.allCheckList[5],self.allCheckList[6],self.allCheckList[7],self.allCheckList[8],self.allCheckList[9],self.allCheckList[10] ]
            return Check_list
        elif assetType == 'lowrig':
            Check_list = [self.allCheckList[1],self.allCheckList[2],self.allCheckList[3],self.allCheckList[4],self.allCheckList[5],self.allCheckList[6],self.allCheckList[7],self.allCheckList[8],self.allCheckList[9],self.allCheckList[10] ]
            return Check_list
            
    def deleteUI(self, window_close = ''):
        """ Check if the window already exist, delete it if True. """

        if mc.window(window_close, exists=True) :
            mc.deleteUI(window_close)

        if mc.windowPref(window_close, exists=True) :
            mc.windowPref(window_close, remove=True)  
            